/**
 * Created by ljw on 2017/4/21.
 */
const  XLSX = require('xlsx');
const _ = require('underscore');
const fs = require('fs');

(async function(){
    try{
        let pathname = `${__dirname}/my` ;
        let fnames = Array.from(fs.readdirSync(pathname)).filter(i=>i.indexOf('xls') != -1);
        console.log('found xls count:',fnames.length);

        let customers = [], uniqCustomers = [], purchases=[], uniqPurchases=[];
        for (let fname of fnames){
            console.log('read',`${pathname}/${fname}`);
            const ws = XLSX.readFileSync(`${pathname}/${fname}`);
            console.log("sheets",ws.SheetNames);

                for(let shtName of ws.SheetNames){
                    for(let line of _.range(3,500)){
                        if(!!ws.Sheets[shtName]['A'+line] && ws.Sheets[shtName]['A'+line].v == ''){
                            break;
                        }else {
                            let customer = {};
                            customer.province = !!ws.Sheets[shtName]['J' + line] ? ws.Sheets[shtName]['J' + line].v : "";
                            customer.city_num = !!ws.Sheets[shtName]['J' + line] ? ws.Sheets[shtName]['J' + line].v : "";
                            customer.center_num = !!ws.Sheets[shtName]['K' + line] ? ws.Sheets[shtName]['K' + line].v : "";
                            customer.customer_manager = !!ws.Sheets[shtName]['L' + line] ? ws.Sheets[shtName]['L' + line].v : "";
                            customer.designer = !!ws.Sheets[shtName]['M' + line] ? ws.Sheets[shtName]['M' + line].v : "";
                            customer.center_manager = !!ws.Sheets[shtName]['N' + line] ? ws.Sheets[shtName]['N' + line].v : "";
                            customer.name = !!ws.Sheets[shtName]['O' + line] ? ws.Sheets[shtName]['O' + line].v : "";
                            customer.full_address = !!ws.Sheets[shtName]['P' + line] ? ws.Sheets[shtName]['P' + line].v : "";
                            customers.push(customer);

                            let purchase ={}, purchase.products=[], product={}, purchase.products.components=[], component={};
                            purchase.order_at = !!ws.Sheets[shtName]['B' + line] ? new Date(1900,0,ws.Sheets[shtName]['B' + line].v) : "";
                            purchase.customer_num = !!ws.Sheets[shtName]['O' + line] ? ws.Sheets[shtName]['O' + line].v : "";
                            purchase.contract_num = !!ws.Sheets[shtName]['Q' + line] ? ws.Sheets[shtName]['Q' + line].v : "";
                            purchase.brand_num = !!ws.Sheets[shtName]['R' + line] ? ws.Sheets[shtName]['R' + line].v : "";
                            purchase.contract_money = !!ws.Sheets[shtName]['S' + line] ? ws.Sheets[shtName]['S' + line].v : "";
                            purchase.plan_arrive_at = !!ws.Sheets[shtName]['T' + line]? (ws.Sheets[shtName]['T' + line].v.toString().indexOf('月')==-1 ? new Date(1900,0,ws.Sheets[shtName]['T' + line].v):ws.Sheets[shtName]['T' + line].v): "";
                            product.product_identity = ;
							component.suitcase_num =;
							purchase.products.components.push(component);
							purchase.products.push(product);
                            purchases.push(purchase)
                        }
                    }
                }
            //客户过滤相同姓名和地址
            uniqCustomers = _.uniq(customers,d=>d.name)
          
            //采购单过滤相同姓名和合同号
            uniqPurchases = _.uniq(purchases,d=>d.customer_num+'_'+d.contract_num)
            
        }

        console.log("客户总数：",uniqCustomers.length);
        console.log("采购单总数：",uniqPurchases.length);
        //输出到json
        fs.writeFileSync(__dirname+'/customer.json',JSON.stringify(uniqCustomers),function(err){
            if(err)	throw err;
        })
        fs.writeFileSync(__dirname+'/purchase.json',JSON.stringify(uniqPurchases),function(err){
            if(err)	throw err;
        })
    }catch(e){
        console.error(e)
    }


    console.log('    ...  exit')
    process.exit(0)

})()
